package com.swt.gap.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 请填写说明
 *
 * @author sx
 * @date 2020/1/19 11:10
 */
public class TimeFormatUtil {

    public static final String SHORT_DATE_FORMATTER = "yyyy-M-d";
    public static final String LONG_DATE_FORMATTER = "yyyy-MM-dd";
    public static final String SHORT_DATETIME_FORMATTER = "yyyy-M-d HH:mm:ss";
    public static final String LONG_DATETIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";

    private static final String SHORT_DATE_REG = "^\\d{4}-((\\d-\\d{1,2})|(\\d{2}-\\d))$";
    private static final String LONG_DATE_REG = "^\\d{4}-\\d{2}-\\d{2}$";
    private static final String SHORT_DATETIME_REG = "^\\d{4}-((\\d-\\d{1,2})|(\\d{2}-\\d)) \\d{2}:\\d{2}:\\d{2}$";
    private static final String LONG_DATETIME_REG = "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}$";

    public static String transformToStandardFormat(String strTime) {
        String retValue = strTime;
        if (null != strTime) {
            if (strTime.matches(SHORT_DATE_REG)) {
                LocalDate date = LocalDate.parse(strTime, DateTimeFormatter.ofPattern(SHORT_DATE_FORMATTER));
                retValue = date.atTime(0, 0, 0).format(DateTimeFormatter.ofPattern(LONG_DATETIME_FORMATTER));
            } else if (strTime.matches(LONG_DATE_REG)) {
                LocalDate date = LocalDate.parse(strTime, DateTimeFormatter.ofPattern(LONG_DATE_FORMATTER));
                retValue = date.atTime(0, 0, 0).format(DateTimeFormatter.ofPattern(LONG_DATETIME_FORMATTER));
            } else if (strTime.matches(SHORT_DATETIME_REG)) {
                LocalDateTime dateTime = LocalDateTime.parse(strTime, DateTimeFormatter.ofPattern(SHORT_DATETIME_FORMATTER));
                retValue = dateTime.format(DateTimeFormatter.ofPattern(LONG_DATETIME_FORMATTER));
            } else if (strTime.matches(LONG_DATETIME_REG)) {
                LocalDateTime dateTime = LocalDateTime.parse(strTime, DateTimeFormatter.ofPattern(LONG_DATETIME_FORMATTER));
                retValue = dateTime.format(DateTimeFormatter.ofPattern(LONG_DATETIME_FORMATTER));
            }
        }
        return retValue;
    }

    public static String getCurrentTimeString(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
    }
}
